package br.com.JZSolution.domineduplex.controle;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import br.com.JZSolution.domineduplex.DomineDuplex;

public class ControladorDePreferencias {
	private Properties properties;
	private static ControladorDePreferencias instance;	
	private File file;

	public static final String PORTA = "campoPortaServer";
	public static final String PASTA = "campoPastaBase";
	public static final String USUARIO = "usuario";
	public static final String ULTIMOPATH = "lastPath";
	
	static{
		instance = new ControladorDePreferencias();
		instance.carregarPreferencias();
	}
	
	private void carregarPreferencias(){
		try {
			properties = new Properties();
			file = new File(DomineDuplex.getInstance().getDiretorioDeConfiguracao()+".domineduplex");
			if(!file.exists())
				file.mkdir();
			file = new File(file.getAbsolutePath()+File.separatorChar+"preferences.dmd");
			if(!file.exists()){
				file.createNewFile();
				inicializarPreferencias();
				properties.storeToXML(new FileOutputStream(file), "");
			}
			
			properties.loadFromXML(new FileInputStream(file));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void inicializarPreferencias() {
		setPropriedade(PORTA, "6041");
		setPropriedade(PASTA, System.getProperty("user.home"));
		setPropriedade(USUARIO, USUARIO);
		setPropriedade(ULTIMOPATH, System.getProperty("user.home"));
	}

	public void salvarPreferencias(){
		try {
			properties.storeToXML(new FileOutputStream(file), "");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private ControladorDePreferencias(){}
	
	
	public static ControladorDePreferencias getInstance() {
		return instance;
	}
	
	public String getPropriedade(String key){
		return properties.getProperty(key);
	}
	
	public void setPropriedade(String key,String valor){
		properties.setProperty(key,valor);
	}
	
	public Properties getProperties() {
		return properties;
	}
	
}
